// pages/home/home.js

import { Home } from "../../models/home";
import { config } from "../../config/config";
import { UserInfo } from "../../models/userinfo";
import Toast from "../../miniprogram_npm/@vant/weapp/toast/toast";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    banner: [
      {
        img: config.imgUrl + "home/banner.png"
      },
      {
        img: config.imgUrl + "home/banner2.png"
      },
      {
        img: config.imgUrl + "home/banner3.png"
      },
      {
        img: config.imgUrl + "home/banner4.png"
      }
    ],
    boutiqueOfficeList: [],
    officeRentList: [],
    productList: [],
    rentValue: "",
    loadingType: "loading",
    userInfo: null,
    config: config
  },

  async init() {
    let postData = {
      PageIndex: 1,
      PageRows: 10,
      SortField: "id",
      SortType: "desc"
    };
    const data = await Home.GetDataList(postData);
    data.Data.map(item => {
      item.time = item.CreateTime.split(" ")[0];
      item.img = item.Imags.split(",")[0];
      return item;
    });
    this.setData({
      productList: data.Data
    });
    console.log(this.data.productList);
  },

  async goPurchaseOrder(event) {
    const id = event.currentTarget.dataset.id;

    wx.navigateTo({
      url: `/pages/purchase-order/purchase-order?id=${id}`
    });
  },
  publishOrder() {
    if (this.data.userInfo.RegistType != "1") {
      Toast("用设计师账号登录后才能发布需求");
      return;
    }
    wx.navigateTo({
      url: "/pages/publish-order/publish-order"
    });
    // this.$router.push({
    //   path: "publish-order"
    // });
  },
  goArticle2() {
    wx.navigateTo({
      url: `../article/article?src=https://mp.weixin.qq.com/s/DpUqcEgonS4Wom7Lhpo96w`
    });
  },
  goArticle1() {
    wx.navigateTo({
      url: `../article/article?src=https://mp.weixin.qq.com/s/QirGn7xY-wcLvDwK47DpSw`
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let userInfo = new UserInfo();
    this.setData({
      userInfo: userInfo.getUserInfo()
    });
    this.init();
  },
  goOffice(event) {
    const type = event.currentTarget.dataset.type;
    wx.navigateTo({
      url: `../office/office?type=${type}`
    });
  },

  goFeatureOffice(event) {
    const id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: `../feature-office/feature-office?id=${id}`
    });
  },

  goGoods() {
    wx.navigateTo({
      url: "../goods/goods"
    });
  },

  scrollToList() {
    var query = wx.createSelectorQuery();
    query.selectViewport().scrollOffset();
    query.select("#recommend").boundingClientRect();
    query.exec(function(res) {
      var miss = res[0].scrollTop + res[1].top;
      wx.pageScrollTo({
        scrollTop: miss,
        duration: 300
      });
    });
  },

  async initOfficeRentList() {
    const paging = HouseList.getOfficeRentList(this.data.rentValue);
    this.data.officeRent = paging;
    const data = await paging.getMoreData();
    if (!data) {
      return;
    }
    data.accumulator.map(item => {
      item;
      return item;
    });
    this.setData({
      officeRentList: data.items
    });
    return data.items;
  },

  onRentClear() {
    wx.pageScrollTo({
      scrollTop: 0
    });
    this.setData({
      rentValue: ""
    });
    this.initOfficeRentList();
  },

  async onRentSearch() {
    await this.initOfficeRentList();
    this.scrollToList();
  },

  onRentChange(e) {
    this.setData({
      rentValue: e.detail
    });
  },

  goWeituo() {
    wx.navigateTo({
      url: "../weituo/weituo"
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: async function() {
    // const data = await this.data.officeRent.getMoreData();
    // if (!data) {
    //   return;
    // }
    // if (!data.moreData) {
    //   this.setData({
    //     loadingType: "end"
    //   });
    // }
    // this.setData({
    //   officeRentList: data.accumulator
    // });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
});
